package com.jml.account.initApplication.listener;

import com.jml.account.sysMenu.entity.SysMenu;
import com.jml.account.sysMenu.service.SysMenuService;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class StartSystem implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext application = sce.getServletContext();
        String contextPath = application.getContextPath();
        application.setAttribute("APP_PATH",contextPath);
        //模拟页面生成验证码加入
        application.setAttribute("validateCode", "1234");
        //2.加载所有许可路径
        WebApplicationContext appctx = WebApplicationContextUtils.getWebApplicationContext(application);
        SysMenuService permissionService = (SysMenuService) appctx.getBean("sysMenuServiceImpl");
        ApplicationContext ioc = WebApplicationContextUtils.getWebApplicationContext(application);
        //PermissionService permissionService = ioc.getBean(PermissionService.class);

        List<SysMenu> queryAllMenu = permissionService.getAllPermission();


        Set<String> allURIs = new HashSet<String>();

        for (SysMenu sysMenu : queryAllMenu) {
            allURIs.add("/"+sysMenu.getUrl());
        }
        application.setAttribute("URLPermission", allURIs);
    }

    public void contextDestroyed(ServletContextEvent sce) {

    }
}
